/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.user.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.supcan.treelist.cols.Group;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.sys.entity.User;
import com.lczyfz.campusinfo.modules.user.entity.CampusinfoGroup;
import com.lczyfz.campusinfo.modules.user.entity.CampusinfoUserGroup;
import com.lczyfz.campusinfo.modules.user.service.CampusinfoGroupService;
import com.lczyfz.campusinfo.modules.user.service.CampusinfoUserGroupService;
import com.lczyfz.campusinfo.modules.user.service.CampusinfoUserService;

/**
 * 用户组别管理Controller
 * @author 陈维乾
 * @version 2018-03-31
 */
@Controller
@RequestMapping(value = "${adminPath}/user/campusinfoUserGroup")
public class CampusinfoUserGroupController extends BaseController {

	@Autowired
	private CampusinfoUserGroupService campusinfoUserGroupService;
	
	@Autowired
	private CampusinfoUserService campusinfoUserService;
	
	@Autowired
	private CampusinfoGroupService campusinfoGroupService;
	
	
	@ModelAttribute
	public CampusinfoUserGroup get(@RequestParam(required=false) String id) {
		CampusinfoUserGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = campusinfoUserGroupService.get(id);
		}
		if (entity == null){
			entity = new CampusinfoUserGroup();
		}
		return entity;
	}
	
	@RequiresPermissions("user:campusinfoUserGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(CampusinfoUserGroup campusinfoUserGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		//TODO
		//讲groupname集合传给view
		List<CampusinfoGroup> campusinfoGroups = campusinfoGroupService.findAllList(new CampusinfoGroup());
		model.addAttribute("campusinfoGroups", campusinfoGroups);
		
		Page<CampusinfoUserGroup> page = campusinfoUserGroupService.findPage(new Page<CampusinfoUserGroup>(request, response), campusinfoUserGroup); 
		model.addAttribute("page", page);
		return "modules/user/campusinfoUserGroupList";
	}

	@RequiresPermissions("user:campusinfoUserGroup:view")
	@RequestMapping(value = "form")
	public String form(CampusinfoUserGroup campusinfoUserGroup, Model model) {

		List<User> users = campusinfoUserService.findAllUser();

		model.addAttribute("users", users);

		List<CampusinfoGroup> campusinfoGroups = campusinfoGroupService.findAllList(new CampusinfoGroup());
		model.addAttribute("campusinfoGroups", campusinfoGroups);
		
		model.addAttribute("campusinfoUserGroup", campusinfoUserGroup);
		return "modules/user/campusinfoUserGroupForm";
	}

	@RequiresPermissions("user:campusinfoUserGroup:edit")
	@RequestMapping(value = "save")
	public String save(CampusinfoUserGroup campusinfoUserGroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, campusinfoUserGroup)){
			return form(campusinfoUserGroup, model);
		}
		int resultCount = campusinfoUserGroupService.isUniqueness(campusinfoUserGroup);
		if(resultCount > 0){
			addMessage(redirectAttributes, "保存用户组别信息失败，请勿重复添加");
			return "redirect:"+Global.getAdminPath()+"/user/campusinfoUserGroup/?repage";
		}

		campusinfoUserGroupService.save(campusinfoUserGroup);
		addMessage(redirectAttributes, "保存用户组别信息成功");
		return "redirect:"+Global.getAdminPath()+"/user/campusinfoUserGroup/?repage";
	}
	
	@RequiresPermissions("user:campusinfoUserGroup:edit")
	@RequestMapping(value = "delete")
	public String delete(CampusinfoUserGroup campusinfoUserGroup, RedirectAttributes redirectAttributes) {
		campusinfoUserGroupService.delete(campusinfoUserGroup);
		addMessage(redirectAttributes, "删除用户组别信息成功");
		return "redirect:"+Global.getAdminPath()+"/user/campusinfoUserGroup/?repage";
	}

}